#!/usr/bin/env ruby

# Quick 'n' dirtry translation of select characters from
# ISO-8859-1 or CP-1252 to ASCII.
# This is too dumb to make sense of UTF-8.

module CharTrans
  TRANSLATES = {
    "\x92" => "'",  # RIGHT_SINGLE_QUOTE
    "\xAD" => '-',  # SOFT_HYPHEN
    "\xC0-\xC4" => 'A',
    "\xE0-\xE4" => 'a',
    "\xC8-\xCB" => 'E',
    "\xE8-\xEB" => 'e',
    "\xCC-\xCF" => 'I',
    "\xEC-\xEF" => 'i',
    "\xD2-\xD6" => 'O',
    "\xF2-\xF6" => 'o',
    "\xD9-\xDC" => 'U',
    "\xF9-\xFC" => 'u',
    "\xC7" => 'C',
    "\xE7" => 'c',
    "\xD1" => 'N',
    "\xF1" => 'n',
  }
  
  SUBS = {
    /\xC6/ => 'AE',
    /\xE6/ => 'ae',
    /\x8C/ => 'OE',  # CP-1252 only?, not in ISO-8859-1?
    /\x9C/ => 'oe',  # CP-1252 only?, not in ISO-8859-1?
    /\xDF/ => 'ss',
  }
  
  def iso2ascii(string)
    string = string.to_s unless string.is_a?(String)
    TRANSLATES.each { |k, v| string = string.tr(k, v) }
    SUBS.each { |k, v| string = string.gsub(k, v) }
    return string
  end
end

########################################

=begin
#doctest Demo CharTrans::iso2ascii
>> include CharTrans
=> Object
>> iso2ascii("ch\xE2teau")
=> "chateau"
>> iso2ascii("\xc6SOP")
=> "AESOP"
>> iso2ascii("to\xADmorrow")
=> "to-morrow"
>> iso2ascii("Hallowe\x92en")
=> "Hallowe'en"
=end
